﻿
$(function () {
  var cs = document.createElement("link");
  cs.setAttribute("rel", "stylesheet");
  cs.setAttribute("type", "text/css");
  cs.setAttribute("href", "/css/usercard.css?v=201556");
  document.getElementsByTagName('head')[0].appendChild(cs);

  bindUserID($(document));
  bindCard($(document));
});

function bindUserID(obj) {
    $(obj).find("a[href^='http://bulo.hujiang.com']").each(function () {
        var $this = $(this);
        if ($this.attr("data-notbindusercard")) {
            return;
        }

        var href = $this.attr("href");
    var userID = href.match(/\d+/);
    var host = href.replace(/(\/u)?\/\d+\/?/ig, "");
    if (userID && host == "http://bulo.hujiang.com") {
        $this.attr("userid", userID);
    }
    });
    
    var prefix = getHostPrefix();
    var myhjUrl = '//'+ prefix + 'my.hujiang.com/pc';
    $(obj).find("a[href^='" + myhjUrl + "']").each(function () {
        var $this = $(this);
        if ($this.attr("data-notbindusercard")) {
            return;
        }

        var href = $this.attr("href");
        var userID = href.match(/\d+/);
        var host = href.replace(/(\/u)?\/\d+\/?/ig, "");
        if (userID && host == myhjUrl) {
            $this.attr("userid", userID);
        }
    });

}

function getHostPrefix() {
    var host = location.host;
    if (/^qa/.test(host) || /^local/.test(host))
        return 'qa';
    else if (/^yz/.test(host))
        return 'yz';
    else
        return '';
}

function bindCard(obj) {
  $(obj).find("[userid]").each(function () {
    bindUserCard($(this));
  });

    try {
        $("a.userface").live("mouseover", function () {
            if (!$(this).attr("bindface")) {
                bindUserCard($(this));
                $(this).mouseover();
            }
        });
    } catch (e) {

    }
}

function addBind(obj) {
  bindUserID(obj);
  bindCard(obj);
}

var timerShowUserFace;
var timerHideUserFace;
var canHideShowUserFace = true;

function bindUserCard(obj) {
  var width = 311;
  var height = 225; //配置信息框大小
  var direction = { x: 0, y: 0 }; //信息框相对于元素的方向
  var location = { x: 0, y: 0 };  //信息框的绝对位置

  $(obj).attr("bindface", "true");

  $(obj).hover(
    function (e) {
      //判断是否有老师信息弹窗，如果有，则优先显示
      var classId = $(this).data('belongToClassid');
      if (window['showTeacherPopupFlag-'+classId]) return;
      // 获取浏览器窗口宽度
      var winWidth = 0;
      if (window.innerWidth) {
        winWidth = window.innerWidth;
      }
      else if ((document.body) && (document.body.clientWidth)) {
        winWidth = document.body.clientWidth;
      }

      var offset = $(this).offset();

      var top = offset.top - $(document).scrollTop(); //元素的绝对上边距
      var right = winWidth - offset.left + $(document).scrollLeft(); //元素的绝对右边距

      if (top > (height + 10)) {
        direction.y = 1; //居上
        location.y = offset.top - height - 5;
      }
      else {
        direction.y = -1; //居下
        location.y = offset.top + $(this).height() + 15;
      }

      if (right < width) {
        direction.x = -1; //居左
        location.x = offset.left + $(this).width() + 5 - width;
      }
      else {
        direction.x = 1; //居右
        location.x = offset.left - 5;
      }

      ClearTimerHideUserFace();
      canHideShowUserFace = false;

      var userId = $(this).attr("userid");

      timerShowUserFace = setTimeout(function () {

        $('#usercard').remove();
        $("body").append("<div id='usercard'><div class='card_loading'><span>正在加载，请稍候...</span></div>");

        //淡入等待窗
        $("#usercard").css({ left: location.x, top: (direction.y == 1 ? location.y + 130 : location.y) }).fadeIn(200);

        if ($("body").data("user_" + userId) == undefined) {
          $.ajax({
            type: "Get",
            dataType: "html",
            url: "/app/user/card.aspx",
            data: { userID: userId },
            success: function (response) {
              $("body").data("user_" + userId, response)
              showCard(response, direction, location, userId);
            },
            timeout: 1000 * 15,
            error: function (e) { }
          });
        }
        else {
        	showCard($("body").data("user_" + userId), direction, location, userId);
        }
      }, 500)
    },
  function () {
    canHideShowUserFace = true;
    ClearTimerShowUserFace();
    timerHideUserFace = setTimeout(function () {
      if (canHideShowUserFace) {
       $('#usercard').fadeOut(200);
      }
    }, 500);
  });
}




function showCard(str, direction, location,userId) {
  $('#usercard').html(str);
  var offset = 40;
  if ($("#card_follow").length > 0) {
    offset =0;
  }
  if ($("#tr_medal").length == 0) {
    $("#usercard").css({ height: $("#usercard").height() - 25 });
    $(".card_info table").css({ height: $(".card_info table").height() - 25 });
    offset += 25;
  }
   if ($("#tr_starAuthInfo").length == 0) {
    //$("#usercard").css({ height: $("#usercard").height() - 25 });
    //$(".card_info table").css({ height: $(".card_info table").height() - 25 });
    offset += 37;
  }

  if (direction.y == 1) {
    $("#usercard").css({ top: location.y +offset });
  }
}

$(".link_faceSendIng").live("click", function () {
	var username = $(this).attr("username");
	ingSenderBox.showSendBox(username);
});

$(".link_faceSendMsg").live("click", function () {
	var username = $(this).attr("username");
	window.open("//bulo.hujiang.com/msg/#/compose/" + username, "_blank");
});

$("#usercard").live("mouseover", function () {
  canHideShowUserFace = false;
}).live("mouseout", function () {
  canHideShowUserFace = true;
  ClearTimerShowUserFace();
  timerHideUserFace = setTimeout(function () {
    if (canHideShowUserFace) {
      $('#usercard').fadeOut(200);
    }
  }, 1000);
});

function ClearTimerShowUserFace() {
  try {
    if (timerShowUserFace) {
     clearTimeout(timerShowUserFace);
    }
  } catch (err) { }
}

function ClearTimerHideUserFace() {
  try {
    if (timerHideUserFace) {
      clearTimeout(timerHideUserFace);
    }
  } catch (err) { }
}


function card_AddFollow(id, name) {
  $("#add_follow").hide();
  $("#cancel_follow").show();
  $.ajax({
    url: "//bulo.hujiang.com/service/GetUserFace.ashx?callback=?",
    dataType: "jsonp",
    data:{op:"follow","FriendID":id,"FriendName":name},
    success: function (data) {
      $("#add_follow").hide();
      $("#cancel_follow").show();

      $("body").data("user_" + id, $('#usercard').html());
    }
  });
}

function card_CancelFollow(id) {
  $.ajax({
    url: "//bulo.hujiang.com/service/GetUserFace.ashx?callback=?",
    dataType: "jsonp",
    data: { op: "cancel", "FriendID": id},
    success: function (data) {
      $("#add_follow").show();
      $("#cancel_follow").hide();
      $("body").data("user_" + id, $('#usercard').html());
    }
  });
}